package com.jerei.plugin.control;

import com.jerei.plugin.entity.WcmCommonAdminuser;
import com.jerei.plugin.service.IWcmCommonAdminuserService;
import com.jerei.plugin.service.IWcmCommonRoleService;
import com.jerei.utils.UtilsCommon;
import com.jerei.utils.UtilsCondition;
import com.jerei.web.plugin.controller.PluginController;
import com.jerei.web.utils.UtilsUI;
import com.jerei.web.utils.UtilsWeb;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import ro.fortsoft.pf4j.Extension;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by sunfeihu on 2016/12/12.
 */
@Extension
public class AdminControl extends PluginController {

    @Autowired
    private IWcmCommonAdminuserService iWcmCommonAdminuserService;
    @Autowired
    private IWcmCommonRoleService iWcmCommonRoleService;

    @RequiresPermissions({"common-adminuser:view"})
    public String adminPage() {
        Long gid = UtilsWeb.getLong(this.getRequest(), "search.eq.gid");
        Integer nowPage = UtilsWeb.getInteger(this.getRequest(), "page");
        Integer pageSize = UtilsWeb.getInteger(this.getRequest(), "rows");
        if (null == gid) {
            return UtilsUI.datagrid();
        }
        Map<String, String[]> paramMap = new HashMap<String, String[]>();
        paramMap.putAll(this.getRequest().getParameterMap());
        paramMap.put("search.eq.gid", new String[]{
                gid + ""
        });
        return UtilsUI.datagrid(iWcmCommonAdminuserService.page(nowPage, pageSize, UtilsCondition.getConditionEntity(paramMap)));
    }

    @RequiresPermissions({"common-adminuser:save", "common-adminuser:update", "common-adminuser:view"})
    public void adminEdit() {
        Long id = UtilsWeb.getLong(this.getRequest(), "id");
        if (null != iWcmCommonAdminuserService && null != id) {
            this.getModel().addAttribute("obj", iWcmCommonAdminuserService.find(id));
        } else {
            Long gid = UtilsWeb.getLong(this.getRequest(), "gid");
            WcmCommonAdminuser entity = new WcmCommonAdminuser();
            entity.setGid(gid);
            this.getModel().addAttribute("obj", entity);
        }
    }

    @RequiresPermissions({"common-adminuser:save", "common-adminuser:update"})
    public String adminSave() {
        String[] roles = this.getRequest().getParameterValues("roles");
        WcmCommonAdminuser entity = this.wrapper(WcmCommonAdminuser.class);
        if (null != iWcmCommonAdminuserService) {
            if (null != entity.getId() && entity.getId() > 0L) {
                iWcmCommonAdminuserService.update(entity);
            } else {
                entity = iWcmCommonAdminuserService.save(entity);
            }
            //实体保存成功以后，保存一下角色
            if (null != roles && roles.length > 0) {
                Long[] roleIds = new Long[roles.length];
                for (int i = 0; i < roles.length; i++) {
                    roleIds[i] = UtilsCommon.getLong(roles[i]);
                }
                iWcmCommonRoleService.saveRoleForAdminuser(entity.getId(), roleIds);
            }
            return UtilsUI.ok();
        }
        return UtilsUI.fail();
    }

}